package com.srit.map.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.srit.core.bean.Page;
import com.srit.core.ibatis.dao.SqlMapDao;
import com.srit.map.business.bean.Role;
import com.srit.map.business.bean.User;
import com.srit.map.business.bean.UserAndRole;
import com.srit.map.business.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired(required = true)
	private SqlMapDao sqlMapDao;

	@Override
	public User getUserByUsername(User user) {
		User tmp = new User();
		tmp.setId(1);
		tmp.setName("admin");
		tmp.setPassword("admin");
		return tmp;
	}

	@Override
	public List getMenuByUser(User user) {
		List list=sqlMapDao.queryForList("User.getMenuByUser", user);
		System.out.print(list.get(0).toString());
		return list;
	}

	@Override
	public int validate(User user) {
		int num=(Integer)sqlMapDao.queryForObject("User.validate", user);
		return num;
	}

	@Override
	public void search(Page page) {
		
		sqlMapDao.queryForPage("User.searchUser", page);
		List<User> list = (List)page.getList();
		List<User> _list = new ArrayList<User>();
		for(User user:list){
			Role role = (Role)sqlMapDao.queryForObject("Role.get_role_by_userid", user.getId());
			user.setRole(role);
			_list.add(user);
		}
		page.setList(_list);
	}

	@Override
	public void save(User user) {
		UserAndRole ur = new UserAndRole();
		ur.setUserId(user.getId());
		ur.setRoleId(user.getRole().getId());
		sqlMapDao.save("Role.", ur);
		sqlMapDao.save("User.save", user);
		
	}

	
	
}
